package tcln.gxtx.client.widget.grid.editor;

import java.util.List;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;

/**
 * Extension of {@link ColumnModel} which correlates with {@link ContextualCellEditor} to support more information and
 * methods for a column model. One of the most important and worthy method is
 * {@link #getEditorByContext(ModelData, String, int, int)}, which looks up a cell editor according to given model data.
 * 
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
public class ExtendedColumnModel extends ColumnModel {

    protected static final String BOLD_BODER_LEFT_CELL = "treegrid-col-border-left";

    /**
     * @param columns
     */
    public ExtendedColumnModel(List<ColumnConfig> columns) {
        super(columns);
    }

    /**
     * Checks if a given column is the last visible one in the container grid/treegrid.
     * 
     * @param column
     * @return <code>true</code> if this model has some columns and the given one is the last visible one.
     */
    public final boolean isLastColumn(ColumnConfig column) {
        boolean isLast = false;
        int colIndex = configs.indexOf(column);

        if (!column.isHidden()) {
            if (colIndex == configs.size() - 1) {
                isLast = true;
            } else {
                isLast = true;
                for (int i = colIndex + 1; i < configs.size(); i++) {
                    if (!configs.get(i).isHidden()) {
                        isLast = false;
                        break;
                    }
                }
            }
        }

        return isLast;
    }

    /**
     * Looks up correct cell editor by context.
     * 
     * @param model
     *            Current tree model.
     * @param property
     *            Affecting property.
     * @param row
     *            Editing row.
     * @param col
     *            Editing column.
     * @return Correct cell editor for given context.
     * @see ContextualCellEditor
     */
    public CellEditor getEditorByContext(ModelData model, String property, int row, int col) {
        CellEditor cellEditor = super.getEditor(col);

        // lookup for cell editor according to current context
        if (cellEditor instanceof ContextualCellEditor) {
            cellEditor = ((ContextualCellEditor) cellEditor).getEditor(model, property, row, col);
        }

        return cellEditor;
    }
}
